﻿using System;
using System.Web;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.ObjectModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;
using BdcAdmin = Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.Office.Server.Utilities;
using Microsoft.BusinessData;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.BusinessData.MetadataModel;

namespace CenterSiteVSTemplateWizard
{
    public partial class EntityDetailsInputForm : Form
    {

        public string entityfieldsroperty=null;
        public static string spportalName = Program.URL;

        public EntityDetailsInputForm()
        {
            InitializeComponent();
        }

        /* adding the nm fields to checkbox,so that ISV can select fields for display in center site*/
        public EntityDetailsInputForm(NameValueCollection nm)
        {
            InitializeComponent();

            
            CheckBox[] cb1= new CheckBox[nm.Count];
            for ( int i=0; i <nm.Count; i++)
            {
                cb1[i] = new CheckBox();
                cb1[i].Text = nm.Keys[i];
                checkedListBox1.Items.Add(cb1[i].Text);
                comboBox1.Items.Add(cb1[i].Text);
            }


        }       
    
        /* ISV Clicked on finish button, so we need to close both forms. */
        private void finishButton1_Click(object sender, EventArgs e)
        {
          
            entityfieldsroperty = generateVLSchema();            
            this.Close();
            Program.form1.Close();

        }
        
        /* genearate the Schema structure for Center site VL based on ISV selection */

        private string generateVLSchema()
        {
            string str;
            str = "        <ViewFields>" + System.Environment.NewLine;

            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                if (comboBox1.Text.CompareTo(checkedListBox1.CheckedItems[i].ToString()) == 0)
                {
                    // this is for the field which we want to show BDC actions ("Open workspace" action)
                    str += "          <FieldRef Name=" + '"' + comboBox1.Text + '"' + " LinkToItem=" + '"' + "TRUE" + '"' + " ListItemMenu=" + '"' + "TRUE" + '"' + "/>";
                    str += System.Environment.NewLine;
                }
                else
                {
                    
                    str += "          <FieldRef Name=" + '"' + checkedListBox1.CheckedItems[i].ToString() + '"' + "/>";
                    str += System.Environment.NewLine;

                }
            }

            // this is for sorting order
            str += "        </ViewFields>" + System.Environment.NewLine + "        <Query>" + System.Environment.NewLine + "         <OrderBy>" + System.Environment.NewLine;
            str += "          <FieldRef Name=" + '"' + comboBox1.Text + '"' + "/>" + System.Environment.NewLine;
            str += "         </OrderBy>" + System.Environment.NewLine + "        </Query>";
            return str;

        }

        // ISV clicked on Previous button on details form
        private void PrevButton1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Program.form1.Show();
            
        }

     

      

 }
}
